package com.ytjj.quartz.service;

import com.ytjj.qmyx.mall.mapper.PlatformStrictDynamicIncomeListMapper;
import com.ytjj.qmyx.mall.model.PlatformStrictDynamicIncomeList;
import com.ytjj.qmyx.mall.model.PlatformStrictDynamicIncomeListExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RefreshScope
@Slf4j
public class PlatformStrictDynamicIncomeListService {
    @Resource
    private PlatformStrictDynamicIncomeListMapper platformStrictDynamicIncomeListMapper;

    public int insert(PlatformStrictDynamicIncomeList platformStrictDynamicIncomeList) {
        return platformStrictDynamicIncomeListMapper.insert(platformStrictDynamicIncomeList);
    }

    public int update(PlatformStrictDynamicIncomeList platformStrictDynamicIncomeList) {
        return platformStrictDynamicIncomeListMapper.updateByPrimaryKey(platformStrictDynamicIncomeList);
    }

    public PlatformStrictDynamicIncomeList queryByUserIdAndCreateTime(Integer userId) {
        PlatformStrictDynamicIncomeListExample example = new PlatformStrictDynamicIncomeListExample();
        PlatformStrictDynamicIncomeListExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        Map<String, Object> timeResult = curDay();
        Date startTime = (Date) timeResult.get("dayStart");
        Date endTime = (Date) timeResult.get("dayEnd");
        criteria.andCreateTimeBetween(startTime, endTime);
        List<PlatformStrictDynamicIncomeList> list = platformStrictDynamicIncomeListMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    /**
     * 查询当天排行榜达人数据
     * @return
     */
    public List<PlatformStrictDynamicIncomeList> queryCurDayRankList() {
        List<PlatformStrictDynamicIncomeList> list = platformStrictDynamicIncomeListMapper.queryCurDayRankList();
        return list;
    }

    /**
     * 今天开始时间到当前时间
     * @return
     */
    public static Map<String, Object> curDay() {
        LocalDate ld = LocalDate.now(ZoneId.systemDefault());
        LocalDateTime localDateTime = ld.atStartOfDay();

        Date startTime = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

        LocalDateTime ldt_end = LocalDateTime.now(ZoneId.systemDefault());
        Date endTime = Date.from(ldt_end.atZone(ZoneId.systemDefault()).toInstant());

        Map<String, Object> result = new HashMap<>();
        result.put("dayStart", startTime);
        result.put("dayEnd", endTime);
        return result;
    }
}
